def get_memory_usage_percentage(system):
    if system.get('memory_total', 0) == 0:
        return 0
    return (system['memory_used'] / system['memory_total']) * 100


def get_disk_usage_percentage(system):
    if system.get('disk_total', 0) == 0:
        return 0
    return (system['disk_used'] / system['disk_total']) * 100


def get_uptime_formatted(system):
    seconds = system.get('uptime', 0)
    days = int(seconds // (24 * 3600))
    hours = int((seconds % (24 * 3600)) // 3600)
    minutes = int((seconds % 3600) // 60)

    if days > 0:
        return f"{days}天{hours}小时{minutes}分钟"
    elif hours > 0:
        return f"{hours}小时{minutes}分钟"
    else:
        return f"{minutes}分钟"


def format_bytes(bytes_value, unit="MB"):
    if unit == "GB":
        if bytes_value < 1024:
            return f"{bytes_value:.2f} GB"
        tb = bytes_value / 1024
        return f"{tb:.2f} TB"

    if bytes_value < 1024:
        return f"{bytes_value:.2f} MB"
    gb = bytes_value / 1024
    return f"{gb:.2f} GB"